package com.gzxxgl.task;

import java.util.ArrayList;
import java.util.Calendar;

import org.apache.log4j.Logger;

import com.gzxxgl.util.DateUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.cron4j.ITask;

/**
 * 年龄、出生日期、工龄
 * 
 * @author hadoop
 *
 */
public class AgeTask implements ITask {
	private static Logger logger = Logger.getLogger(AgeTask.class);

	@Override
	public void run() {

		/**
		 * 核算年龄
		 */
		java.util.List<Record> r = Db.find("select * from g_user_info where isdelete = 0");
		java.util.List<Record> list = new ArrayList<>();

		String age = "", gl = "", csrq;
		for (Record o : r) {
			// 身份证号码大于15位
			if (o != null && StrKit.notBlank(o.getStr("sfzh")) && o.getStr("sfzh").length() > 15) {
				Calendar cal = Calendar.getInstance();

				int nyear = cal.get(Calendar.YEAR);

				age = (nyear - Integer.parseInt(o.getStr("sfzh").substring(6, 10))) + "";

				String s = o.getStr("rzrq");
				if (StrKit.notBlank(s) && !"null".equals(s)) {
					gl = DateUtil.gl(DateUtil.getDate(s, "yyyy-MM-dd"));
				}
				csrq = o.getStr("sfzh").substring(6, 14);

				try {
					o.set("gage", age);
					o.set("csrq", DateUtil.getString(csrq, "yyyyMMdd", "yyyy-MM-dd"));
					o.set("gl", gl);
					list.add(o);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}

		int[] ret = null;
		if (list.size() > 0) {
			ret = Db.batchUpdate("g_user_info", "infoid", list, 300);
		}
		int s = 0, l = 0;
		if (ret != null) {
			for (int i = 0; i < ret.length; i++) {
				if (ret[i] >= 0)
					s++;
				else if (ret[i] >= -2)
					s++;
				else
					l++;
			}
		}
		logger.error("更新成功，总" + ret.length + "条数据   更新 " + s + "行数据，丢失" + l + "行数据 ");
		System.out.println("更新成功，总" + ret.length + "条数据   更新 " + s + "行数据，丢失" + l + "行数据 ");
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		int nyear = cal.get(Calendar.YEAR);
		System.out.println(nyear+Integer.parseInt("620403199306120011".substring(6, 14)));
	}
}
